﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RH.Web.ViewModels;
using RH.Domain;
using RH.Common.Extensions;

namespace RH.Web.Utility
{
    public static class ApplicationCache
    {
        private const String CATEGORIES = "Categories";
        private const String USER_GROUPS = "UserGroups";
        /// <summary>
        /// Caches the Categories
        /// </summary>
        public static List<CategoryViewModel> Categories
        {
            get
            {
                List<CategoryViewModel> categories = HttpContext.Current.Application[CATEGORIES] as List<CategoryViewModel>;
                if (categories == null || categories.Count == 0)
                {
                    RefreshCategoryCache();
                    categories = HttpContext.Current.Application[CATEGORIES] as List<CategoryViewModel>;
                }
                return categories;
            }
        }

        public static void RefreshCategoryCache()
        {
            AppEntities context = new AppEntities();
            List<Category> categories = context.Categories.Include("ParentCategory").Include("ChildCategories").ToList();
            HttpContext.Current.Application[CATEGORIES] = AutoMapper.Mapper.Map<List<Category>, List<CategoryViewModel>>(categories);
            //_categories = AutoMapper.Mapper.Map<List<Category>, List<CategoryViewModel>>(context.Categories.Include("ParentCategory").ToList());
        }
        public static List<GroupViewModel> UserGroups
        {
            get
            {
                List<GroupViewModel> userGroups = HttpContext.Current.Application[USER_GROUPS] as List<GroupViewModel>;
                if (userGroups == null || userGroups.Count == 0)
                {
                    RefreshUserGroups();
                    userGroups = HttpContext.Current.Application[USER_GROUPS] as List<GroupViewModel>;
                }
                return userGroups;
            }
        }
        public static void RefreshUserGroups()
        {
            AppEntities context = new AppEntities();
            List<Group> groups = context.Groups.ToList();
            List<GroupViewModel> groupsViewModel = AutoMapper.Mapper.Map<List<Group>, List<GroupViewModel>>(groups);
            HttpContext.Current.Application[USER_GROUPS] = groupsViewModel;
        }

        public static String CompanyRoutes
        {
            get
            {
                String routes = HttpContext.Current.Application["COMPANY_ROUTES"] as String;
                if (routes.IsEmpty() || String.Compare(routes, "undefined", true) == 0)
                {
                    RefreshCompanyRouteCache(false);
                    routes = HttpContext.Current.Application["COMPANY_ROUTES"] as String;
                }
                return routes;
            }
        }

        public static void RefreshCompanyRouteCache(bool forceRebuild)
        {
            String routes = WebUtil.BuildCompanyUrlRoutes(forceRebuild);
            HttpContext.Current.Application["COMPANY_ROUTES"] = routes;
        }
        public static String ContentRoutes
        {
            get
            {
                String routes = HttpContext.Current.Application["CONTENT_ROUTES"] as String;
                if (routes.IsEmpty() || String.Compare(routes, "undefined", true) == 0)
                {
                    RefreshContentRouteCache(false);
                    routes = HttpContext.Current.Application["CONTENT_ROUTES"] as String;
                }
                return routes;
            }
        }

        public static void RefreshContentRouteCache(bool forceRebuild)
        {
            String routes = WebUtil.BuildContentUrlRoutes(forceRebuild);
            HttpContext.Current.Application["CONTENT_ROUTES"] = routes;
        }   
    }
}